﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Newtonsoft.Json;
using Container = NetJsWire.Lite.Widgets.UI.Container;

namespace NetJsWire.Lite.Designers
{
    public class ContainerDesigner : ParentControlDesigner
    {
        protected ISelectionService SelectionServ;
        private Container _container;
        private DesignerVerbCollection _mVerbs;

        protected Pen BorderPen
        {
            get
            {
                var color = ( Control.BackColor.GetBrightness() < 0.5 ) ? ControlPaint.Light( Control.BackColor ) : ControlPaint.Dark( Control.BackColor );
                var pen = new Pen( color ) { DashStyle = DashStyle.Dash };
                return pen;
            }
        }

        public override DesignerVerbCollection Verbs
        {
            get
            {
                return _mVerbs ??
                       ( _mVerbs = new DesignerVerbCollection { new DesignerVerb( "Get javascript", OnGetJavascript ) } );
            }
        }

        public override void Initialize( IComponent component )
        {
            base.Initialize( component );
            _container = ( Container ) component;

            SelectionServ = GetService( typeof( ISelectionService ) ) as ISelectionService;
        }
        protected virtual void DrawBorder( Graphics graphics )
        {
            var component = ( Container ) base.Component;
            if ( component == null ) return;

            var borderPen = BorderPen;
            var clientRectangle = Control.ClientRectangle;
            clientRectangle.Width--;
            clientRectangle.Height--;
            graphics.DrawRectangle( borderPen, clientRectangle );
            borderPen.Dispose();
        }

        protected override void OnPaintAdornments( PaintEventArgs pe )
        {
            if ( Control is Container )
            {
                //DrawBorder( pe.Graphics );
            }
            base.OnPaintAdornments( pe );
        }

        // Properties

        protected override void WndProc( ref Message m )
        {
            // Filter out titlebar clicks
            //
            switch ( ( Native.Msg ) m.Msg )
            {
                case Native.Msg.WM_NCMBUTTONDBLCLK:
                case Native.Msg.WM_NCMBUTTONDOWN:
                case Native.Msg.WM_NCRBUTTONDBLCLK:
                case Native.Msg.WM_NCRBUTTONDOWN:
                case Native.Msg.WM_NCLBUTTONDBLCLK:
                case Native.Msg.WM_NCLBUTTONDOWN:
                case Native.Msg.WM_NCLBUTTONUP:
                    if ( SelectionServ != null )
                    {
                        var lParam = m.LParam.ToInt32();
                        var x = ( lParam << 16 ) >> 16; // lo order word
                        var y = lParam >> 16; // hi order word
                        var screenPoint = new Point( x, y );
                        var pt = _container.PointToClient( screenPoint );
                        var selComp = _container.GetComponentAtPoint( pt );
                        SelectionServ.SetSelectedComponents( new object[] { selComp ?? Component } );
                        // Mark message as handled
                        m.Result = selComp != null ? IntPtr.Zero : new IntPtr( -1 );
                    }
                    break;
                case Native.Msg.WM_NCHITTEST:
                    var lParam2 = m.LParam.ToInt32();
                    var x2 = ( lParam2 << 16 ) >> 16; // lo order word
                    var y2 = lParam2 >> 16; // hi order word
                    var screenPoint2 = new Point( x2, y2 );
                    var pt2 = _container.PointToClient( screenPoint2 );
                    if ( !_container.ClientRectangle.Contains( pt2 ) )
                    {
                        m.Result = new IntPtr( 18 );//HTBORDER
                    }
                    else
                    {
                        base.WndProc( ref m );
                    }
                    break;
                default:
                    base.WndProc( ref m );
                    break;
            }
        }

        protected void OnGetJavascript( object sender, EventArgs e )
        {
            Clipboard.SetText( ( ( Container ) Component ).Serialize( Formatting.Indented ).Replace( "___store", "store" ) );
            MessageBox.Show( "The Javascript code is on the clipboard", "System" );
        }
    }
}
